package com.tjrac.generalpermissions.service.impl;

import com.tjrac.filter.entity.impl.ListParam;
import com.tjrac.generalpermissions.dao.RoleDao;
import com.tjrac.generalpermissions.dao.UserDao;
import com.tjrac.generalpermissions.domain.User;
import com.tjrac.generalpermissions.epception.MsgException;
import com.tjrac.generalpermissions.epception.statusCode.LoginCode;
import com.tjrac.generalpermissions.service.UserService;
import com.tjrac.generalpermissions.utils.JsonPack;
import com.tjrac.generalpermissions.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ZeNing
 * @create 2020-11-22 17:17
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    UserDao userDao;

    @Resource
    RoleDao roleDao;

    @Override
    public User login(String username, String password) throws MsgException {
        User user = userDao.queryByUsername(username);

        if (user == null) {
            throw new MsgException(LoginCode.NOT_USER, "账户不存在!");
        } else {
            String userPassword = user.getPassword();
            String userMD5Password = MD5.MD5Encrypt(password);
            if (!Objects.equals(userMD5Password, userPassword)) {
                throw new MsgException(LoginCode.ERROR_PASSWORD, "密码错误!");
            }
        }
        return user;
    }

    @Override
    public String getCurrentUserInfo(HttpSession session) throws MsgException {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            JsonPack jsonPack = new JsonPack();
            jsonPack.put("username", user.getUsername());
            jsonPack.put("role_name", user.getRole().getName());
            return new MsgException(LoginCode.SUCCESS_USER, "查询当前用户信息成功").toOneJson(jsonPack);
        } else {
            throw new MsgException(LoginCode.FAILURE_GET_USER_INFO, "获取当前用户信息失败");
        }
    }

    @Override
    public String logout(HttpSession session) throws MsgException {
        session.invalidate();
        return "";
    }

    @Override
    public String queryUser(HttpSession session) throws MsgException {
        User user = (User) session.getAttribute("user");
        JsonPack jsonPack = new JsonPack();

        //如果该用户为root
        if (user != null) {
            if (user.getUsername().equals("root")) {
                List<User> users = userDao.queryAll();
                ArrayList<String> usernames = new ArrayList<>();
                ArrayList<String> roleNames = new ArrayList<>();
                for (User u : users) {
                    usernames.add(u.getUsername());
                    roleNames.add(u.getRole().getName());
                }
                jsonPack.put("usernames", usernames);
                jsonPack.put("role_names", roleNames);
                return new MsgException(LoginCode.SUCCESS_USER, "查询所有用户信息成功").toOneJson(jsonPack);
            } else {
                jsonPack.put("username", user.getUsername());
                jsonPack.put("role_name", user.getRole().getName());
                return new MsgException(LoginCode.SUCCESS_USER, "查询用户信息成功").toOneJson(jsonPack);
            }
        } else {
            throw new MsgException(LoginCode.FAILURE_GET_USER_INFO, "获取当前用户信息失败");
        }

    }

    @Override
    public ListParam queryAllListParam() {
        List<Object> names = new ArrayList<>();
        List<User> users = userDao.queryAll();
        for (User user : users){
            names.add(user.getUsername());
        }
        return new ListParam(names);
    }
}
